/**
 * @author Javen 
 * @copyright 2018-12-07 15:51:19 javendev@126.com 
 * @description QQ玩一玩示例 
 */

let BKTools = require("./common/BKTools");
let NetUtils = require("./common/NetUtils");
let Global = require("./common/Global");
let customComponent = require("./common/CustomComponent");

cc.Class({
    extends: customComponent,

    properties: {
        tipNode: {
            default: null,
            type: cc.Node,
            tooltip: "提示节点",
            visible: true
        },
    },
    toBack() {
        cc.director.loadScene("Scene");
    },

    setTipMsg(msg) {
        let lable = this.tipNode.getComponent(cc.Label);
        lable.string = msg;
        this.scheduleOnce(function () {
            lable.string = "请点击按钮测试";
        }, 3);
    },

    btnClick(event, data) {
        this.playClickAudio(); //CustomComponent 父类的方法
        if (cc.sys.platform != cc.sys.QQ_PLAY &&
            data != "log" && data != "get" && data != "post") {
            this.setTipMsg("请在玩一玩环境下调试");
            BKTools.log("请在玩一玩环境下调试:" + data);
            return;
        }


        switch (data) {
            case "systemInfo":
                BKTools.log("获取系统信息...");
                this._getSystemInfoSync();
                break;
            case "screenShotShare":
                BKTools.log("截图分享...");
                BKTools.screenShotShare();
                break;
            case "shareToArk":
                BKTools.log("单渠道分享...");
                BKTools.shareToArk();
                break;
            case "share":
                BKTools.log("多渠道分享...");
                BKTools.share();
                break;
            case "shareLink":
                BKTools.log("分享链接或者图片...");
                BKTools.shareLink();
                break;

            case "follow":
                BKTools.log("关注...");
                BKTools.follow();
                break;
            case "log":
                BKTools.log("IJPay让支付触手可及", "Gitee:https://gitee.com/Javen205/IJPay");
                break;
            case "QRCode":
                //异步接口    
                BK.QRCode.makeQRCodeToFile({
                    path: 'GameSandBox://qrcode.png',
                    content: 'https://gitee.com/javen205/IJPay',
                    config: {
                        width: 128,
                        height: 128,
                        colorDark: {
                            r: 0.0,
                            g: 0.0,
                            b: 0.0,
                            a: 1.0
                        },
                        colorLight: {
                            r: 1.0,
                            g: 1.0,
                            b: 1.0,
                            a: 1.0
                        },
                        correctLevel: BK.QRErrorCorrectLevel.H
                    },
                    success: function (succObj) {
                        BKTools.log("二维码生成成功 path:" + succObj.path);
                    },
                    fail: function (err) {
                        BKTools.log("失败了，失败原因：" + err.msg);
                    },
                    complete: function () {
                        BKTools.log('完成');
                    }
                });

                BK.QRCode.makeQRCodeToFileSync({
                    path: 'GameSandBox://qrcode2.png',
                    content: 'IJPay让支付触手可及',
                    config: {
                        width: 256,
                        height: 256
                    }
                });
                break;
            case "localStorage":
                //手q 版本7.8.5
                BK.localStorage.setItem('name', 'Javen');
                BKTools.log("localStorage name:" + BK.localStorage.getItem('name'));

                BKTools.log("localStorage stringKey:" + BK.localStorage.key(0));

                break;
            case "removeLocalStorage":
                BK.localStorage.setItem('name', 'Javen');
                BK.localStorage.setItem('project', 'IJPay 让支付触手可及 https://gitee.com/javen205/IJPay');

                BKTools.log("name,project:" + BK.localStorage.getItem('name'), BK.localStorage.getItem('project'));

                BK.localStorage.removeItem('name');
                BKTools.log("name,project:" + BK.localStorage.getItem('name'), BK.localStorage.getItem('project'));

                BK.localStorage.clear();

                BKTools.log("removeLocalStorage name,project:" + BK.localStorage.getItem('name'), BK.localStorage.getItem('project'));

                break;
            case "sessionStorage":
                //手q 版本7.8.5
                BK.sessionStorage.setItem('name', 'Javen');
                break;
            case "getSessionStorage":
                //手q 版本7.8.5

                BKTools.log("sessionStorage name:" + BK.sessionStorage.getItem('name'));

                BKTools.log("sessionStorage stringKey:" + BK.sessionStorage.key(0));

                break;
            case "removeSessionStorage":
                BK.sessionStorage.setItem('name', 'Javen');
                BK.sessionStorage.setItem('project', 'IJPay 让支付触手可及 https://gitee.com/javen205/IJPay');

                BKTools.log("name,project:" + BK.sessionStorage.getItem('name'), BK.sessionStorage.getItem('project'));

                BKTools.log("stringKey:" + BK.sessionStorage.key(1));

                BK.localStorage.removeItem('name');
                BKTools.log("name,project:" + BK.localStorage.getItem('name'), BK.localStorage.getItem('project'));

                BK.sessionStorage.clear();

                BKTools.log("removeSessionStorage name,project:" + BK.sessionStorage.getItem('name'), BK.sessionStorage.getItem('project'));

                break;
            case "toast":
                this.showToast("IJPay了解一下");
                break;
            case "showLoading":
                this.showLoading("数据加载中...");
                this.scheduleOnce(function () {
                    this.hideLoading("数据加载中...");
                }, 5);
                break;
            case "showAlert":
                this.showAlert("IJPay", "IJPay 让支付触手可及");
                break;
            case "audio":
                Global.isAudio = !Global.isAudio;
                if (Global.isAudio) {
                    this._audioClip.playBgmAudio();
                } else {
                    this._audioClip.stopAll();
                }
                break;
            case "adVideo":
                let videoAd = Global.videoAd;
                if (videoAd) {
                    videoAd.show();
                    videoAd.onPlayStart(function () {
                        BKTools.log("video ad onPlayStart...");
                        //QQ玩一玩或者是引擎的bug 播放广告时没有关闭背景音效需要手动处理
                        if (Global.isAudio) {
                            this._audioClip.pauseAll();
                        }
                    }.bind(this));
                    videoAd.onPlayFinish(function () {
                        //播放结束
                        BKTools.log("video ad onPlayFinish...");
                    }.bind(this));
                    videoAd.onClose(function () {
                        BKTools.log("video ad  onClose...");
                        if (Global.isAudio) {
                            this._audioClip.resumeAll();
                        }
                    }.bind(this));
                } else {
                    BKTools.log("暂无广告资源.....");
                    BKTools.loadVideoAd();
                }
                break;
            case "banner":
                BKTools.showBannerAd(1002);
                break;
            case "hideBanner":
                BKTools.hideBannerAd();
                break;
            case "post":
                NetUtils.post("https://httpbin.org/anything?key=value", "", function (status, data) {
                    BKTools.log("post请求>" + status, JSON.stringify(data))
                });
                break;
            case "get":
                NetUtils.get("http://www.wanandroid.com/tools/mockapi/3461/Javen", "", function (status, data) {
                    BKTools.log("get请求>" + status, JSON.stringify(data))
                });
                break;
            case "bkpost":
                BK.Http.request({
                    url: 'https://httpbin.org/anything?key=value',
                    method: 'POST',
                    headers: {
                        'Referer': 'https://hudong.qq.com',
                        'User-Agent': 'brick-client',
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        "key": "value"
                    }),
                    success: function (succObj) {
                        BKTools.log('statusCode:' + succObj.statusCode);
                        BKTools.log('headers:' + JSON.stringify(succObj.headers));
                        const bodyStr = succObj.text();
                        BKTools.log('body:' + bodyStr);
                    },
                    fail: function (errObj) {
                        BKTools.log('error:' + errObj.msg);
                    },
                    complete: function () {
                        BKTools.log('complete');
                    },
                    uploadProgress: function (curr, total) {
                        BKTools.log('upload progress:' + curr / total);
                    },
                    downloadProgress: function (curr, total) {
                        BKTools.log('download progress:' + curr / total);
                    }
                });
                break;
            case "bkget":
                BK.Http.request({
                    url: 'http://www.wanandroid.com/tools/mockapi/3461/Javen',
                    method: 'GET',
                    success: function (succObj) {
                        BKTools.log('statusCode:' + succObj.statusCode);
                        BKTools.log('headers:' + JSON.stringify(succObj.headers));
                        const bodyStr = succObj.text();
                        BKTools.log('body:' + bodyStr);
                    },
                    fail: function (errObj) {
                        BKTools.log('error:' + errObj.msg);
                    },
                    complete: function () {
                        BKTools.log('complete');
                    },
                    uploadProgress: function (curr, total) {
                        BKTools.log('upload progress:' + curr / total);
                    },
                    downloadProgress: function (curr, total) {
                        BKTools.log('download progress:' + curr / total);
                    }
                });
                break;
            case 'rank':
                BKTools.getRankList(function (errorCode, rankList) {
                    if (errorCode == 0) {
                        if (rankList && rankList.length > 0) {
                            rankList.forEach(element => {
                                BKTools.log("item:" + JSON.stringify(element));
                            });
                        } else {
                            BKTools.log("....排行榜为空....");
                        }
                    } else {
                        BKTools.log("....获取排行榜失败....");
                    }
                }.bind(this));
                break;
            default:
                break;
        }
    },
    showToast(msg) {
        BKTools.showToast(msg);
    },
    showLoading(msg) {
        BK.UI.showLoading({
            title: msg,
            complete: function () {
                BKTools.log("complete show");
            }
        });
    },
    hideLoading(msg) {
        BK.UI.hideLoading({
            title: msg,
            complete: function () {
                BKTools.log("complete hide");
            }
        });
    },
    showAlert(titleStr, contentStr) {
        BK.UI.showAlert({
            title: titleStr,
            content: contentStr,
            success: function (succObj) {
                if (succObj.confirm) {
                    BKTools.log("click confirm");
                } else if (succObj.cancel) {
                    BKTools.log("click cancel");
                }
            },
            complete: function () {
                BKTools.log("complete show");
            }
        });
    },

    _getSystemInfoSync() {
        var systemInfo = BK.getSystemInfoSync();
        BKTools.log('游戏版本号:' + systemInfo.gameVersion);
        BKTools.log('是否房主:' + systemInfo.isMaster); //使用厘米秀房间时才有效
        BKTools.log('房间号:' + systemInfo.roomId); //使用厘米秀房间时才有效
        BKTools.log('游戏id:' + systemInfo.gameId); //使用厘米秀房间时才有效
        BKTools.log('系统版本:' + systemInfo.osVersion); //系统版本 10.3
        BKTools.log('网络类型:' + systemInfo.networkType); //网络类型 1 电信 ，2 联通 ，3 移动 0: wifi或未知

        BKTools.log('平台:' + systemInfo.platform);
        BKTools.log('当前用户的标识:' + systemInfo.openId);
        BKTools.log('手机qq版本:' + systemInfo.QQVer);

        BKTools.log('是否首次安装:' + systemInfo.isFirstInstall);
        BKTools.log('当前聊天窗类型:' + systemInfo.aioType);
        BKTools.log('游戏启动入口:' + systemInfo.src);
        BKTools.log('是否为白名单用户:' + systemInfo.isWhiteUser);
        BKTools.log('游戏类型:' + systemInfo.gameType);
        BKTools.log('具体机型:' + systemInfo.model);
        BKTools.log('性别:' + systemInfo.sex);
        BKTools.log('devPlatform:' + systemInfo.devPlatform);
        BKTools.log('gameParam:' + systemInfo.gameParam);

        //尝试使用旧的方式获取  目前测试还可以使用此方式获取 2018-10-22 
        BKTools.log("openId:" + GameStatusInfo.openId);
        BKTools.log("gameParam:" + GameStatusInfo.gameParam);
    },

    // onLoad () {},

    start() {
        this._super();
        this.playBgmAudio();
    },

    // update (dt) {},
});